using BW.WMS.Application.Features.Basic.Interfaces;
using BW.WMS.Application.Features.Basic.Services;
using BW.WMS.Application.Features.Outbound.Interfaces;
using BW.WMS.Application.Features.Outbound.Services;
using BW.WMS.Application.Features.Product.Interfces;
using BW.WMS.Application.Features.Product.Services;
using BW.WMS.Application.Features.Supperss.Interfice;
using BW.WMS.Application.Features.Supperss.Services;
using BW.WMS.Application.Features.User.Interfaces;
using BW.WMS.Application.Features.User.Services;
using BW.WMS.Application.Features.Warehouse.Interfaces;
using BW.WMS.Application.Features.Warehouse.Services;
using BW.WMS.Domain.Repositories;
using BW.WMS.Infrastructure.Data.Contexts;
using BW.WMS.Infrastructure.Data.Repositories;
using BW.WMS.Infrastructure.Services;
using StackExchange.Redis;
using Yitter.IdGenerator;
using static BW.WMS.Infrastructure.Data.Contexts.DBContext;
using BW.WMS.Application.Features.Product.Interfces;
using BW.WMS.Application.Features.Product.Services;
using BW.WMS.Application.Features.Basic.Interfaces;
using BW.WMS.Application.Features.Basic.Services;
using BW.WMS.Application.Features.Location.Interfaces;
using BW.WMS.Application.Features.Location.Services;
using BW.WMS.Application.Features.Bins.Interfaces;
using BW.WMS.Application.Features.Bin.Services;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(x => { x.IncludeXmlComments(AppContext.BaseDirectory + "/BW.WMS.API.xml", true); });
// 1. ȡݿַļ
var damengConnectionString = builder.Configuration.GetConnectionString("con");

var mySqlConnectionString = builder.Configuration.GetConnectionString("con");

// Redis集群配置
var redisConnectionString = builder.Configuration.GetSection("Redis:ConnectionString").Value;
var isCluster = builder.Configuration.GetValue<bool>("Redis:Configuration:IsCluster", false);

builder.Services.AddSingleton<IConnectionMultiplexer>(provider =>
{
    var configuration = ConfigurationOptions.Parse(redisConnectionString!);
    
    // 基础配置
    configuration.ConnectTimeout = builder.Configuration.GetValue<int>("Redis:Configuration:ConnectTimeout", 10000);
    configuration.SyncTimeout = builder.Configuration.GetValue<int>("Redis:Configuration:SyncTimeout", 10000);
    configuration.ConnectRetry = builder.Configuration.GetValue<int>("Redis:Configuration:ConnectRetry", 3);
    configuration.AbortOnConnectFail = builder.Configuration.GetValue<bool>("Redis:Configuration:AbortOnConnectFail", false);
    configuration.AllowAdmin = builder.Configuration.GetValue<bool>("Redis:Configuration:AllowAdmin", true);
    
    // 集群模式配置
    if (isCluster)
    {
        // 集群模式下使用默认数据库0，不设置DefaultDatabase
        // Redis集群只支持数据库0
    }
    else
    {
        configuration.DefaultDatabase = builder.Configuration.GetValue<int>("Redis:Configuration:DefaultDatabase", 0);
    }
    
    return ConnectionMultiplexer.Connect(configuration);
});

builder.Services.AddScoped<IRedisCacheService, RedisCacheService>();


builder.Services.AddScoped<DBContext>(provider =>
    new DBContext(mySqlConnectionString));

builder.Services.AddScoped(typeof(DataRepository<>));
builder.Services.AddScoped<IOutboundStatisticsService, OutboundStatisticsService>();
builder.Services.AddScoped<IUserServices, UserServices>();
builder.Services.AddScoped<IProductService, ProductService>();
YitIdHelper.SetIdGenerator(new IdGeneratorOptions());


builder.Services.AddScoped(typeof(DataRepository<>)); 


builder.Services.AddScoped<ISupperServices, SupperProductServices>();
//注册仓库管理相关服务
builder.Services.AddScoped<IWMS_WarehouseService, WMS_WarehouseService>();
builder.Services.AddScoped<IWMS_LocationService, WMS_LocationService>();
//注册货位管理相关服务  
builder.Services.AddScoped<IWMS_BinService, WMS_BinService>();

var app = builder.Build();

//
app.UseCors(x=>x.AllowAnyHeader().AllowAnyOrigin().AllowAnyMethod());
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseCors("AllowVueFrontend");

app.UseHttpsRedirection();

// 添加静态文件支持，用于访问上传的文件
app.UseStaticFiles();

app.UseAuthorization();

app.MapControllers();

app.Run();

System.Net.ServicePointManager.SecurityProtocol = 
    System.Net.SecurityProtocolType.Tls12 | 
    System.Net.SecurityProtocolType.Tls13;
